package com.tony.cloud.data.provider;

import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableEurekaClient//t配置本应用将使用服务注册和服务发现，注意：注册和发现用这一个注解。
public class ProviderBootstrap {
	public static void main(String[] args) {
		SpringApplication.run(ProviderBootstrap.class, args);
	}
}
@RestController
class RemoteHelloController {

    //Eureka会自动注入注册的所有Client信息，不过并没有啥用处
    @Autowired
    private DiscoveryClient client;
    
    @Autowired
    private Environment env; 

    @RequestMapping("hello")
    public String hello() {
        long start = System.currentTimeMillis();
        ServiceInstance instance = client.getLocalServiceInstance();
	// 随机睡眠1000毫秒以内
        try {
            Thread.sleep(new Random().nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long cost = System.currentTimeMillis() - start;
        return env.getProperty("mysqldb.datasource.url")+",Remote nima bi Hello~ " + instance.getHost() + ", " + instance.getServiceId()
              + ", spent " + cost;
    }
}